package cnu.ldc.tts.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import cnu.ldc.jdbctest.JdbcTemplate;
import cnu.ldc.tts.model.User;
import cnu.ldc.util.DBconnection;

public class BuyTicketDao {
	public  static int addUser(User user) {
		int rowNum=0;
		String sql="insert into tts_userInfo values(trainInfo_id.nextval,?,?,?,?,?)";
		Connection con=DBconnection.getConnection();
	
		try {
			
			PreparedStatement pst=con.prepareStatement(sql);
			pst.setString(1,user.getUserName() );
			pst.setString(2, user.getPassword());
			pst.setString(3, user.getSex());
			pst.setString(4, user.getIdNumber());
			pst.setString(5, user.getPhone());
			rowNum=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rowNum;		
	}

	public static List<User>  buyticket(String name,String password) {
	
		String sql="select * from tts_userInfo where userName=? and password=?";
		List<User>  list=JdbcTemplate.query(sql, (rs)->{

			List<User>  list0=new ArrayList<>();
			try{
				while(rs.next()) {
				User u=new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),
						rs.getString(5),rs.getString(6));
				list0.add(u);
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return list0;
			
		
			
		}, name,password);
				
		return list;
	}

}
